package tech.aistar.corejava.day15.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:创建一个定长线程池，支持定时及周期性任务执行
 * @date 2019/8/3 0003
 */
public class ScheduledThreadPoolDemo {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        //希望程序延时一秒
//        scheduledExecutorService.schedule(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println(Thread.currentThread().getName()+",延时一秒执行!");
//            }
//        },1, TimeUnit.SECONDS);

        //延时1秒执行,然后后面是每过三秒执行一次!
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+",正在执行!");
            }
        },1,3,TimeUnit.SECONDS);

    }
}
